package com.geo.geotool.config;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.geotools.data.DataStore;
import org.geotools.data.DataStoreFinder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PreDestroy;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description: GeoTools配置类
 * @author : 逃离地球
 *
 */
@Slf4j
@Configuration
public class GeoToolsConfig {
    @Value("${dataStore.dbType}")
    private String dbType;
    @Value("${dataStore.host}")
    private String host;
    @Value("${dataStore.port}")
    private String port;
    @Value("${dataStore.schema}")
    private String schema;
    @Value("${dataStore.database}")
    private String database;
    @Value("${dataStore.user}")
    private String user;
    @Value("${dataStore.passwd}")
    private String passwd;
    
    private DataStore dataStore;
    @Bean
    public DataStore dataStore() {
        Map<String, String> connectionParameters = new HashMap<>();
        connectionParameters.put("dbtype", dbType);
        connectionParameters.put("host", host);
        connectionParameters.put("port", port);
        connectionParameters.put("schema", schema);
        connectionParameters.put("database", database);
        connectionParameters.put("user", user);
        connectionParameters.put("passwd", passwd);
        try {
            dataStore = DataStoreFinder.getDataStore(connectionParameters);
            return dataStore;
        } catch (IOException e) {
            throw new RuntimeException("Unable to create DataStore.", e);
        }
    }

//    @PreDestroy
//    public void dispose() {
//        if (dataStore != null) {
//            dataStore.dispose();
//        }
//    }

}
